clear all
set more off, permanently

capture log close

********************************************************************************
************************* import and clean csv data ****************************
***************************** time varying *************************************

*********** import all csv, clean and drop duplicates, merge all year ************

********************* average temperatures

import delimited "...\tavg2001_VTP1km.csv", delimiter(",") encoding(UTF-8) clear 
drop cid
rename id_1k ID1
rename rastervalu tavg2001
destring tavg2001, dpcomma replace

save "...\tavg01_VTP1km.dta", replace

clear

import delimited "...\tavg2002_VTP1km.csv", delimiter(",") encoding(UTF-8) clear 
drop cid
rename id_1k ID1
rename rastervalu tavg2002

save "...\tavg02_VTP1km.dta", replace


clear

import delimited "...\tavg2003_VTP1km.csv", delimiter(",") encoding(UTF-8) clear 
drop cid
rename id_1k ID1
rename rastervalu tavg2003

save "...\tavg03_VTP1km.dta", replace


clear

import delimited "...\tavg2004_VTP1km.csv", delimiter(",") encoding(UTF-8) clear 
drop cid
rename id_1k ID1
rename rastervalu tavg2004

save "...\tavg04_VTP1km.dta", replace


clear

import delimited "...\tavg2005_VTP1km.csv", delimiter(",") encoding(UTF-8) clear 
drop cid
rename id_1k ID1
rename rastervalu tavg2005

save "...\tavg05_VTP1km.dta", replace


clear

import delimited "...\tavg2006_VTP1km.csv", delimiter(",") encoding(UTF-8) clear 
drop cid
rename id_1k ID1
rename rastervalu tavg2006

 save "...\tavg06_VTP1km.dta", replace


clear

import delimited "...tavg2007_VTP1km.csv", delimiter(",") encoding(UTF-8) clear 
drop cid
rename id_1k ID1
rename rastervalu tavg2007

save "...\tavg07_VTP1km.dta", replace

clear

import delimited "...\tavg2008_VTP1km.csv", delimiter(",") encoding(UTF-8) clear 
drop cid
rename id_1k ID1
rename rastervalu tavg2008

save "...\tavg08_VTP1km.dta", replace


clear

import delimited "...\tavg2009_VTP1km.csv", delimiter(",") encoding(UTF-8) clear 
drop cid
rename id_1k ID1
rename rastervalu tavg2009

save "...\tavg09_VTP1km.dta", replace


clear

import delimited "...\tavg2010_VTP1km.csv", delimiter(",") encoding(UTF-8) clear 
drop cid
rename id_1k ID1
rename rastervalu tavg2010

save "...\tavg10_VTP1km.dta", replace

clear

import delimited "...\tavg2011_VTP1km.csv", delimiter(",") encoding(UTF-8) clear 
drop cid
rename id_1k ID1
rename rastervalu tavg2011

save "...\tavg11_VTP1km.dta", replace

clear

import delimited "...\tavg2012_VTP1km.csv", delimiter(",") encoding(UTF-8) clear 
drop cid
rename id_1k ID1
rename rastervalu tavg2012

save "...\tavg12_VTP1km.dta", replace

merge 1:1 ID1 using "...\tavg01_VTP1km.dta", nogen
merge 1:1 ID1 using "...\tavg02_VTP1km.dta", nogen
merge 1:1 ID1 using "...\tavg03_VTP1km.dta", nogen
merge 1:1 ID1 using "...\tavg04_VTP1km.dta", nogen
merge 1:1 ID1 using "...\tavg05_VTP1km.dta", nogen
merge 1:1 ID1 using "...\tavg06_VTP1km.dta", nogen
merge 1:1 ID1 using "...\tavg07_VTP1km.dta", nogen
merge 1:1 ID1 using "...\tavg08_VTP1km.dta", nogen
merge 1:1 ID1 using "...\tavg09_VTP1km.dta", nogen
merge 1:1 ID1 using "...\tavg10_VTP1km.dta", nogen
merge 1:1 ID1 using "...\tavg11_VTP1km.dta", nogen

save "...\tavg_VTP1km.dta", replace


********************* GFC forest cover and tree cover losses

clear

import delimited "...\TC2000_VTP1km.csv", delimiter(",") encoding(UTF-8) clear 
drop cid
rename id_1k ID1
rename rastervalu treecover2000
duplicates tag ID1, gen(dup)
drop in 1027839
save  "...\TC2000_VTP1km.dta", replace

clear

import delimited "...\lossyear_VTP1km.csv", delimiter(",") encoding(UTF-8) clear 

drop cid

rename id_1k ID1
gen treecoverloss=1 if rastervalu>0
replace treecoverloss=0 if rastervalu==0

rename rastervalu year
replace year=2001 if year==1
replace year=2002 if year==2
replace year=2003 if year==3
replace year=2004 if year==4
replace year=2005 if year==5
replace year=2006 if year==6
replace year=2007 if year==7
replace year=2008 if year==8
replace year=2009 if year==9
replace year=2010 if year==10
replace year=2011 if year==11
replace year=2012 if year==12
replace year=2013 if year==13
replace year=2014 if year==14
replace year=2015 if year==15
replace year=2000 if year==0

drop if year==-9999
drop in 1027960
drop dup

reshape wide treecoverloss, i(ID1) j(year)

save  "...\lossyear_VTP1km.dta", replace

merge 1:1 ID1 using "...\TC2000_VTP1km.dta", nogen

save "...\forest_VTP1km.dta", replace


********************* nighttime lights

clear

import delimited "...\light_f1400_VTP1km.csv", delimiter(",") encoding(UTF-8) clear 
drop cid
rename id_1k ID1
rename rastervalu lightsF142000
duplicates tag ID1, gen(dup)
br if dup==1
drop dup
save "...\lightsF1400_VTP1km.dta", replace

clear

import delimited "...\light_f1500_VTP1km.csv", delimiter(",") encoding(UTF-8) clear 
drop cid
rename id_1k ID1
rename rastervalu lightsF152000
duplicates tag ID1, gen(dup)
br if dup==1
drop dup
save "...\lightsF1500_VTP1km.dta", replace

clear

import delimited "...\light_f1501_VTP1km.csv", delimiter(",") encoding(UTF-8) clear 
drop cid
rename id_1k ID1
rename rastervalu lightsF152001
duplicates tag ID1, gen(dup)
br if dup==1
drop dup
save "...\lightsF1501_VTP1km.dta", replace

clear

import delimited "...\light_f1502_VTP1km.csv", delimiter(",") encoding(UTF-8) clear 
drop cid
rename id_1k ID1
rename rastervalu lightsF152002
duplicates tag ID1, gen(dup)
br if dup==1
drop dup
save "...\lightsF1502_VTP1km.dta", replace

clear

import delimited "...\light_f1503_VTP1km.csv", delimiter(",") encoding(UTF-8) clear 
drop cid
rename id_1k ID1
rename rastervalu lightsF152003
duplicates tag ID1, gen(dup)
br if dup==1
drop in 1028114
save "...\lightsF1503_VTP1km.dta", replace

clear

import delimited "...\light_f1504_VTP1km.csv", delimiter(",") encoding(UTF-8) clear 
drop cid
rename id_1k ID1
rename rastervalu lightsF152004
duplicates tag ID1, gen(dup)
br if dup==1
drop dup
save "...\lightsF1504_VTP1km.dta", replace

clear

import delimited "...\light_f1505_VTP1km.csv", delimiter(",") encoding(UTF-8) clear 
drop cid
rename id_1k ID1
rename rastervalu lightsF152005
duplicates tag ID1, gen(dup)
br if dup==1
drop in 1028081
drop dup
save "...\lightsF1505_VTP1km.dta", replace

clear

import delimited "...\light_f1506_VTP1km.csv", delimiter(",") encoding(UTF-8) clear 
drop cid
rename id_1k ID1
rename rastervalu lightsF152006
duplicates tag ID1, gen(dup)
br if dup==1
drop in 1028080
drop dup
save "...\lightsF1506_VTP1km.dta", replace

clear

import delimited "...\light_f1507_VTP1km.csv", delimiter(",") encoding(UTF-8) clear 
drop cid
rename id_1k ID1
rename rastervalu lightsF152007
duplicates tag ID1, gen(dup)
br if dup==1
drop dup
save "...\lightsF1507_VTP1km.dta", replace

clear

import delimited "...\light_f1604_VTP1km.csv", delimiter(",") encoding(UTF-8) clear 
drop cid
rename id_1k ID1
rename rastervalu lightsF162004
duplicates tag ID1, gen(dup)
br if dup==1
drop in 1028053
drop dup
save "...\lightsF1604_VTP1km.dta", replace

clear

import delimited "...\light_f1605_VTP1km.csv", delimiter(",") encoding(UTF-8) clear 
drop cid
rename id_1k ID1
rename rastervalu lightsF162005
duplicates tag ID1, gen(dup)
br if dup==1
drop dup
save "...\lightsF1605_VTP1km.dta", replace

clear

import delimited "...\light_f1606_VTP1km.csv", delimiter(",") encoding(UTF-8) clear 
drop cid
rename id_1k ID1
rename rastervalu lightsF162006
duplicates tag ID1, gen(dup)
br if dup==1
drop in 1028013
drop dup
save "...\lightsF1606_VTP1km.dta", replace

clear

import delimited "...\light_f1607_VTP1km.csv", delimiter(",") encoding(UTF-8) clear 
drop cid
rename id_1k ID1
rename rastervalu lightsF162007
duplicates tag ID1, gen(dup)
br if dup==1
drop in 1027996
drop dup
save "...\lightsF1607_VTP1km.dta", replace

clear

import delimited "...\light_f1608_VTP1km.csv", delimiter(",") encoding(UTF-8) clear 
drop cid
rename id_1k ID1
rename rastervalu lightsF162008
duplicates tag ID1, gen(dup)
br if dup==1
drop in 1028000
drop dup
save "...\lightsF1608_VTP1km.dta", replace

clear

import delimited "...light_f1609_VTP1km.csv", delimiter(",") encoding(UTF-8) clear 
drop cid
rename id_1k ID1
rename rastervalu lightsF162009
duplicates tag ID1, gen(dup)
br if dup==1
drop in 1028007
drop dup
save "...\lightsF1609_VTP1km.dta", replace

clear

import delimited "...\light_f1810_VTP1km.csv", delimiter(",") encoding(UTF-8) clear 
drop cid
rename id_1k ID1
rename rastervalu lightsF182010
duplicates tag ID1, gen(dup)
br if dup==1
drop dup
save "...\lightsF1810_VTP1km.dta", replace

clear

import delimited "...\light_f1811_VTP1km.csv", delimiter(",") encoding(UTF-8) clear 
drop cid
rename id_1k ID1
rename rastervalu lightsF182011
duplicates tag ID1, gen(dup)
br if dup==1
drop in 1027928
drop dup
save "...\lightsF1811_VTP1km.dta", replace

clear

import delimited "...\light_f1812_VTP1km.csv", delimiter(",") encoding(UTF-8) clear 
drop cid
rename id_1k ID1
rename rastervalu lightsF182012
duplicates tag ID1, gen(dup)
br if dup==1
drop dup
save "...\lightsF1812_VTP1km.dta", replace

clear

import delimited "...\light_f1813_VTP1km.csv", delimiter(",") encoding(UTF-8) clear 
drop cid
rename id_1k ID1
rename rastervalu lightsF182013
duplicates tag ID1, gen(dup)
br if dup==1
drop in 1027856
drop dup
save "...\lightsF1813_VTP1km.dta", replace

merge 1:1 ID1 using "...\lightsF1812_VTP1km.dta", nogen
merge 1:1 ID1 using "...\lightsF1811_VTP1km.dta", nogen
merge 1:1 ID1 using "...\lightsF1810_VTP1km.dta", nogen
merge 1:1 ID1 using "...\lightsF1609_VTP1km.dta", nogen
merge 1:1 ID1 using "...\lightsF1608_VTP1km.dta", nogen
merge 1:1 ID1 using "...\lightsF1607_VTP1km.dta", nogen
merge 1:1 ID1 using "...\lightsF1606_VTP1km.dta", nogen
merge 1:1 ID1 using "...\lightsF1605_VTP1km.dta", nogen
merge 1:1 ID1 using "...\lightsF1604_VTP1km.dta", nogen
merge 1:1 ID1 using "...\lightsF1507_VTP1km.dta", nogen
merge 1:1 ID1 using "...\lightsF1506_VTP1km.dta", nogen
merge 1:1 ID1 using "...\lightsF1505_VTP1km.dta", nogen
merge 1:1 ID1 using "...\lightsF1504_VTP1km.dta", nogen
merge 1:1 ID1 using "...\lightsF1503_VTP1km.dta", nogen
merge 1:1 ID1 using "...\lightsF1502_VTP1km.dta", nogen
merge 1:1 ID1 using "...\lightsF1501_VTP1km.dta", nogen
merge 1:1 ID1 using "...\lightsF1500_VTP1km.dta", nogen
merge 1:1 ID1 using "...\lightsF1400_VTP1km.dta", nogen

drop dup

* for nighttime light that have two satellite observation per year, we take the average:
egen light2000=rowmean(lightsF152000 lightsF142000)
rename lightsF152001 light2001
drop lightsF152000 lightsF142000
rename lightsF152002 light2002
rename lightsF152003 light2003
egen light2004=rowmean(lightsF152004 lightsF162004)
drop lightsF152004 lightsF162004
egen light2005=rowmean(lightsF152005 lightsF162005)
drop lightsF152005 lightsF162005
egen light2006=rowmean(lightsF152006 lightsF162006)
drop lightsF152006 lightsF162006
egen light2007=rowmean(lightsF152007 lightsF162007)
drop lightsF152007 lightsF162007
rename lightsF162008 light2008
rename lightsF162009 light2009
rename lightsF182010 light2010
rename lightsF182011 light2011
rename lightsF182013 light2013
rename lightsF182012 light2012

save "...\lights_VTP1km.dta", replace

********************* roads

clear

import delimited "...\d1km_roads1996.csv", delimiter(";") encoding(UTF-8) clear 
gen ID1 = in_fid+1
drop objectid in_fid
rename near_dist dist_roads1996
rename near_fid near_roads1996
destring dist_roads1996, dpcomma replace
replace dist_roads1996=dist_roads1996/1000
save "...\d1km_roads1996.dta", replace 

clear

import delimited "...\d1km_roads2008.csv", delimiter(";") encoding(UTF-8) clear 
gen ID1 = in_fid+1
drop objectid in_fid
rename near_dist dist_roads2008
rename near_fid near_roads2008
destring dist_roads2008, dpcomma replace
replace dist_roads2008=dist_roads2008/1000
save "...\d1km_roads2008.dta", replace 

merge 1:1 ID1 using "...\d1km_roads1996.dta", nogen

save "...\d1km_roads.dta", replace 

********************* PADDD

clear

import delimited "...\sec1km_PADDD.csv", delimiter(",") encoding(UTF-8) 

drop objectid in_fid near_fid_1 v35 fid_sample cid fid_iba_ut geodatatyp region country iso_3166_1 wdpa_id pa_code protected protecte_1 paddd_reve reversal_y 

rename  id_1k ID1
rename padddtrack PADDD
rename location_k location_k
rename type typePADDD
rename status statusPADDD
rename year_gazet year_gazet
rename year_paddd year
rename cause causePADDD
rename area_paddd areaPADDD
rename size_pre_p sizeprePADDD
rename size_post_ sizepostPADDD
rename iucn_categ IUCNprePADDD
rename iucn_cat_1 IUCNpostPADDD 
rename offset_ offsetPADDD
rename systemic_c sys_change
rename near_dis_1 dist_PADedge

duplicates tag ID1 year, gen(dup)

br if dup==1
drop if PADDD==696 & year==2010 & ID1==54132
drop if PADDD==750 & year==2006 & ID1==315335
drop if PADDD==763 & year==2006 & ID1==492529
drop if PADDD==736 & year==2006 & ID1==331394
drop if PADDD==743 & year==2006 & ID1==301371
drop if PADDD==743 & year==2006 & ID1==633725
drop if PADDD==719 & year==2014 & ID1==559731
drop if PADDD==719 & year==2014 & ID1==194562
drop if PADDD==719 & year==2014 & ID1==916802
drop if PADDD==719 & year==2014 & ID1==1001689
drop if PADDD==719 & year==2014 & ID1==121324
drop if PADDD==719 & year==2014 & ID1==550868
drop if PADDD==719 & year==2014 & ID1==338296
drop if PADDD==719 & year==2014 & ID1==191137
drop if PADDD==719 & year==2014 & ID1==396186
drop if PADDD==780 & year==2013 & ID1==808313
drop if PADDD==765 & year==2013 & ID1==850231
drop if PADDD==780 & year==2013 & ID1==653651
drop if PADDD==765 & year==2013 & ID1==273044
drop if PADDD==780 & year==2013 & ID1==276164
drop if PADDD==719 & year==2014 & ID1==777475
drop if PADDD==719 & year==2014 & ID1==296902
drop if PADDD==719 & year==2014 & ID1==512438

br if dup>1
drop if ID1==0 & PADDD==691 & year==1981

keep PADDD year ID1 

reshape wide PADDD, i(ID1) j(year)

 save "...\1km_PADDD.dta", replace


******************** distance to PADDD each year

clear

import delimited "...\d1km_PADDD_b00.csv", delimiter(";") encoding(UTF-8) clear 
gen ID1 = in_fid+1
drop objectid in_fid
rename near_dist distY_PADDD1999
rename near_fid nearY_PADDD1999
destring distY_PADDD1999, dpcomma replace
replace distY_PADDD1999=distY_PADDD1999/1000
save "...\d1km_PADDDb00.dta", replace 

clear

import delimited "...\d1km_PADDD_00.csv", delimiter(";") encoding(UTF-8) clear 
gen ID1 = in_fid+1
drop objectid in_fid
rename near_dist distY_PADDD2000
rename near_fid nearY_PADDD2000
destring distY_PADDD2000, dpcomma replace
replace distY_PADDD2000=distY_PADDD2000/1000
save "...\d1km_PADDD00.dta", replace 

clear

import delimited "...\d1km_PADDD_01.csv", delimiter(";") encoding(UTF-8) clear 
gen ID1 = in_fid+1
drop objectid in_fid
rename near_dist distY_PADDD2001
rename near_fid nearY_PADDD2001
destring distY_PADDD2001, dpcomma replace
replace distY_PADDD2001=distY_PADDD2001/1000
save "...\d1km_PADDD01.dta", replace 

clear

import delimited "...\d1km_PADDD_02.csv", delimiter(";") encoding(UTF-8) clear 
gen ID1 = in_fid+1
drop objectid in_fid
rename near_dist distY_PADDD2002
rename near_fid nearY_PADDD2002
destring distY_PADDD2002, dpcomma replace
replace distY_PADDD2002=distY_PADDD2002/1000
save "...\d1km_PADDD02.dta", replace 


clear

import delimited "...\d1km_PADDD_03.csv", delimiter(";") encoding(UTF-8) clear 
gen ID1 = in_fid+1
drop objectid in_fid
rename near_dist distY_PADDD2003
rename near_fid nearY_PADDD2003
destring distY_PADDD2003, dpcomma replace
replace distY_PADDD2003=distY_PADDD2003/1000
save "...\d1km_PADDD03.dta", replace 

clear

import delimited "...\d1km_PADDD_04.csv", delimiter(";") encoding(UTF-8) clear 
gen ID1 = in_fid+1
drop objectid in_fid
rename near_dist distY_PADDD2004
rename near_fid nearY_PADDD2004
destring distY_PADDD2004, dpcomma replace
replace distY_PADDD2004=distY_PADDD2004/1000
save "...\d1km_PADDD04.dta", replace 

clear

import delimited "...\d1km_PADDD_05.csv", delimiter(";") encoding(UTF-8) clear 
gen ID1 = in_fid+1
drop objectid in_fid
rename near_dist distY_PADDD2005
rename near_fid nearY_PADDD2005
destring distY_PADDD2005, dpcomma replace
replace distY_PADDD2005=distY_PADDD2005/1000
save "...\d1km_PADDD05.dta", replace 

clear

import delimited "...\d1km_PADDD_06.csv", delimiter(";") encoding(UTF-8) clear 
gen ID1 = in_fid+1
drop objectid in_fid
rename near_dist distY_PADDD2006
rename near_fid nearY_PADDD2006
destring distY_PADDD2006, dpcomma replace
replace distY_PADDD2006=distY_PADDD2006/1000
save "...\d1km_PADDD06.dta", replace 

clear

import delimited "...\d1km_PADDD_07.csv", delimiter(";") encoding(UTF-8) clear 
gen ID1 = in_fid+1
drop objectid in_fid
rename near_dist distY_PADDD2007
rename near_fid nearY_PADDD2007
destring distY_PADDD2007, dpcomma replace
replace distY_PADDD2007=distY_PADDD2007/1000
save "...\d1km_PADDD07.dta", replace 

clear

import delimited "...\d1km_PADDD_08.csv", delimiter(";") encoding(UTF-8) clear 
gen ID1 = in_fid+1
drop objectid in_fid
rename near_dist distY_PADDD2008
rename near_fid nearY_PADDD2008
destring distY_PADDD2008, dpcomma replace
replace distY_PADDD2008=distY_PADDD2008/1000
save "...\d1km_PADDD08.dta", replace 

clear

import delimited "...\d1km_PADDD_09.csv", delimiter(";") encoding(UTF-8) clear 
gen ID1 = in_fid+1
drop objectid in_fid
rename near_dist distY_PADDD2009
rename near_fid nearY_PADDD2009
destring distY_PADDD2009, dpcomma replace
replace distY_PADDD2009=distY_PADDD2009/1000
save "...\d1km_PADDD09.dta", replace 

clear

import delimited "...\d1km_PADDD_10.csv", delimiter(";") encoding(UTF-8) clear 
gen ID1 = in_fid+1
drop objectid in_fid
rename near_dist distY_PADDD2010
rename near_fid nearY_PADDD2010
destring distY_PADDD2010, dpcomma replace
replace distY_PADDD2010=distY_PADDD2010/1000
save "...\d1km_PADDD10.dta", replace 

clear

import delimited "...\d1km_PADDD_11.csv", delimiter(";") encoding(UTF-8) clear 
gen ID1 = in_fid+1
drop objectid in_fid
rename near_dist distY_PADDD2011
rename near_fid nearY_PADDD2011
destring distY_PADDD2011, dpcomma replace
replace distY_PADDD2011=distY_PADDD2011/1000
 save "...\d1km_PADDD11.dta", replace 

clear

import delimited "...\d1km_PADDD_12.csv", delimiter(";") encoding(UTF-8) clear 
gen ID1 = in_fid+1
drop objectid in_fid
rename near_dist distY_PADDD2012
rename near_fid nearY_PADDD2012
destring distY_PADDD2012, dpcomma replace
replace distY_PADDD2012=distY_PADDD2012/1000
save "...\d1km_PADDD12.dta", replace 

clear

import delimited "...\d1km_PADDD_13.csv", delimiter(";") encoding(UTF-8) clear 
gen ID1 = in_fid+1
drop objectid in_fid
rename near_dist distY_PADDD2013
rename near_fid nearY_PADDD2013
destring distY_PADDD2013, dpcomma replace
replace distY_PADDD2013=distY_PADDD2013/1000
save "...\d1km_PADDD13.dta", replace 

clear

import delimited "...\d1km_PADDD_14.csv", delimiter(";") encoding(UTF-8) clear 
gen ID1 = in_fid+1
drop objectid in_fid
rename near_dist distY_PADDD2014
rename near_fid nearY_PADDD2014
destring distY_PADDD2014, dpcomma replace
replace distY_PADDD2014=distY_PADDD2014/1000
save "...\d1km_PADDD14.dta", replace 

merge 1:1 ID1 using  "...\d1km_PADDD13.dta", nogen
merge 1:1 ID1 using  "...\d1km_PADDD12.dta", nogen
merge 1:1 ID1 using  "...\d1km_PADDD11.dta", nogen
merge 1:1 ID1 using  "...\d1km_PADDD10.dta", nogen
merge 1:1 ID1 using  "...\d1km_PADDD09.dta", nogen
merge 1:1 ID1 using  "...\d1km_PADDD08.dta", nogen
merge 1:1 ID1 using  "...\d1km_PADDD07.dta", nogen
merge 1:1 ID1 using  "...\d1km_PADDD06.dta", nogen
merge 1:1 ID1 using  "...\d1km_PADDD05.dta", nogen
merge 1:1 ID1 using  "...\d1km_PADDD04.dta", nogen
merge 1:1 ID1 using  "...\d1km_PADDD03.dta", nogen
merge 1:1 ID1 using  "...\d1km_PADDD02.dta", nogen
merge 1:1 ID1 using  "...\d1km_PADDD01.dta", nogen
merge 1:1 ID1 using  "...\d1km_PADDD00.dta", nogen
merge 1:1 ID1 using  "...\d1km_PADDDb00.dta", nogen
merge 1:1 ID1 using  "...\1km_PADDD.dta", nogen

save "...\1km_PADDD.dta", replace

******************** PAs

clear

import delimited "...\sec1km_wdpa.csv", delimiter(",") encoding(UTF-8) 

keep id_1kn1911 status_yrn100 fid_iba_utn100 iucn_catc80

duplicates tag id_1kn1911, gen(dup)

replace iucn_catc80="1" if iucn_catc80=="Ia"
replace iucn_catc80="2" if iucn_catc80=="II"
replace iucn_catc80="3" if iucn_catc80=="III"
replace iucn_catc80="4" if iucn_catc80=="IV"
replace iucn_catc80="5" if iucn_catc80=="V"
replace iucn_catc80="6" if iucn_catc80=="VI"
replace iucn_catc80="." if iucn_catc80=="Not Reported"
destring iucn_cat, dpcomma replace

destring iucn_cat, dpcomma replace

gen iucn=iucn_cat if dup==1
bysort id_1k: egen IUCN=max(iucn)
drop if dup==1 & iucn>=IUCN
drop dup iucn IUCN

duplicates tag id_1kn1911, gen(dup)
gen iucn=iucn_catc80 if dup==2
bysort id_1kn1911: egen IUCN=max(iucn)
drop if dup==2 & iucn>=IUCN
drop dup iucn IUCN

duplicates tag id_1kn1911, gen(dup)
duplicates drop id_1kn1911, force
drop dup iucn_catc80

rename id_1kn191 ID1
rename fid_iba_utn100 PA
rename status_yrn100 year

reshape wide PA, i(ID1) j(year)


save "...\1km_PA.dta", replace



******************** Rainfall 



import delimited "...\chprec2000_VTP1km.csv", delimiter(",") encoding(UTF-8) clear 
drop cid
rename id_1k ID1
rename rastervalu _prec2000
duplicates tag ID1, gen(dup)
br if dup==1
drop dup
 save "...\CHprec2000_VTP1km.dta", replace


clear


import delimited "...\chprec2001_VTP1km.csv", delimiter(",") encoding(UTF-8) clear 
drop cid
rename id_1k ID1
rename rastervalu _prec2001
duplicates tag ID1, gen(dup)
br if dup==1
drop dup
 save "...\CHprec2001_VTP1km.dta", replace

import delimited  "...\chprec2002_VTP1km.csv", delimiter(",") encoding(UTF-8) clear 
drop cid
rename id_1k ID1
rename rastervalu _prec2002
duplicates tag ID1, gen(dup)
br if dup==1
drop dup
 save "...\CHprec2002_VTP1km.dta", replace

import delimited "...\chprec2003_VTP1km.csv", delimiter(",") encoding(UTF-8) clear 
drop cid
rename id_1k ID1
rename rastervalu _prec2003
duplicates tag ID1, gen(dup)
br if dup==1
drop dup
save "...\CHprec2003_VTP1km.dta", replace

import delimited "...\chprec2004_VTP1km.csv", delimiter(",") encoding(UTF-8) clear 
drop cid
rename id_1k ID1
rename rastervalu _prec2004
duplicates tag ID1, gen(dup)
br if dup==1
drop dup
save "...\CHprec2004_VTP1km.dta", replace

import delimited "...\chprec2005_VTP1km.csv", delimiter(",") encoding(UTF-8) clear 
drop cid
rename id_1k ID1
rename rastervalu _prec2005
duplicates tag ID1, gen(dup)
br if dup==1
drop dup
save "...\CHprec2005_VTP1km.dta", replace

import delimited "...\chprec2006_VTP1km.csv", delimiter(",") encoding(UTF-8) clear 
drop cid
rename id_1k ID1
rename rastervalu _prec2006
duplicates tag ID1, gen(dup)
br if dup==1
drop dup
save "...\CHprec2006_VTP1km.dta", replace

import delimited "...\chprec2007_VTP1km.csv", delimiter(",") encoding(UTF-8) clear 
drop cid
rename id_1k ID1
rename rastervalu _prec2007
duplicates tag ID1, gen(dup)
br if dup==1
drop dup
save "...\CHprec2007_VTP1km.dta", replace

import delimited "...\chprec2008_VTP1km.csv", delimiter(",") encoding(UTF-8) clear 
drop cid
rename id_1k ID1
rename rastervalu _prec2008
duplicates tag ID1, gen(dup)
br if dup==1
drop dup
save "...\CHprec2008_VTP1km.dta", replace

import delimited "...\chprec2009_VTP1km.csv", delimiter(",") encoding(UTF-8) clear 
drop cid
rename id_1k ID1
rename rastervalu _prec2009
duplicates tag ID1, gen(dup)
br if dup==1
save "...\CHprec2009_VTP1km.dta", replace
 
import delimited "...\chprec2010_VTP1km.csv", delimiter(",") encoding(UTF-8) clear 
drop cid
rename id_1k ID1
rename rastervalu _prec2010
duplicates tag ID1, gen(dup)
br if dup==1
drop dup
save "...\CHprec2010_VTP1km.dta", replace

import delimited "...\chprec2011_VTP1km.csv", delimiter(",") encoding(UTF-8) clear 
drop cid
rename id_1k ID1
rename rastervalu _prec2011
duplicates tag ID1, gen(dup)
br if dup==1
drop dup
save "...\CHprec2011_VTP1km.dta", replace

import delimited "...\chprec2012_VTP1km.csv", delimiter(",") encoding(UTF-8) clear 
drop cid
rename id_1k ID1
rename rastervalu _prec2012
duplicates tag ID1, gen(dup)
br if dup==1
drop dup
save "...\CHprec2012_VTP1km.dta", replace

import delimited "...\chprec2013_VTP1km.csv", delimiter(",") encoding(UTF-8) clear 
drop cid
rename id_1k ID1
rename rastervalu _prec2013
duplicates tag ID1, gen(dup)
br if dup==1
drop dup
save "...\CHprec2013_VTP1km.dta", replace

import delimited "...\chprec2014_VTP1km.csv", delimiter(",") encoding(UTF-8) clear 
drop cid
rename id_1k ID1
rename rastervalu _prec2014
duplicates tag ID1, gen(dup)
br if dup==1
drop dup
save "...\CHprec2014_VTP1km.dta", replace

 
import delimited "...\chprec2015_VTP1km.csv", delimiter(",") encoding(UTF-8) clear 
drop cid
rename id_1k ID1
rename rastervalu _prec2015
duplicates tag ID1, gen(dup)
br if dup==1
drop dup
save "...\CHprec2015_VTP1km.dta", replace

merge 1:1 ID1 using "...\CHprec2000_VTP1km.dta", nogen
merge 1:1 ID1 using "...\CHprec2001_VTP1km.dta", nogen
merge 1:1 ID1 using "...\CHprec2002_VTP1km.dta", nogen
merge 1:1 ID1 using "...\CHprec2003_VTP1km.dta", nogen
merge 1:1 ID1 using "...\CHprec2004_VTP1km.dta", nogen
merge 1:1 ID1 using "...\CHprec2005_VTP1km.dta", nogen
merge 1:1 ID1 using "...\CHprec2006_VTP1km.dta", nogen
merge 1:1 ID1 using "...\CHprec2007_VTP1km.dta", nogen
merge 1:1 ID1 using "...\CHprec2008_VTP1km.dta", nogen
merge 1:1 ID1 using "...\CHprec2009_VTP1km.dta", nogen
merge 1:1 ID1 using "...\CHprec2010_VTP1km.dta", nogen
merge 1:1 ID1 using "...\CHprec2011_VTP1km.dta", nogen
merge 1:1 ID1 using "...\CHprec2012_VTP1km.dta", nogen
merge 1:1 ID1 using "...\CHprec2013_VTP1km.dta", nogen
merge 1:1 ID1 using "...\CHprec2014_VTP1km.dta", nogen

save "...\CHprec_VTP1km.dta", replace

clear

******************* Agricultural fires

clear

import delimited "...\fires_2001_VTP1km.csv", delimiter(",") encoding(UTF-8) clear 
drop cid
rename id_1k ID1
rename rastervalu agrifires2001
duplicates tag ID1, gen(dup)
br if dup==1
drop in 1028018
drop dup
 save "...\agrifires01_VTP1km.dta", replace

clear

import delimited "...\fires_2002_VTP1km.csv", delimiter(",") encoding(UTF-8) clear 
drop cid
rename id_1k ID1
rename rastervalu agrifires2002
duplicates tag ID1, gen(dup)
br if dup==1
drop dup
 save "...\agrifires02_VTP1km.dta", replace

clear

import delimited "...\fires_2003_VTP1km.csv", delimiter(",") encoding(UTF-8) clear 
drop cid
rename id_1k ID1
rename rastervalu agrifires2003
duplicates tag ID1, gen(dup)
br if dup==1
drop dup
 save "...\agrifires03_VTP1km.dta", replace

clear

import delimited "...\fires_2004_VTP1km.csv", delimiter(",") encoding(UTF-8) clear 
drop cid
rename id_1k ID1
rename rastervalu agrifires2004
duplicates tag ID1, gen(dup)
br if dup==1
drop dup
 save "...\agrifires04_VTP1km.dta", replace

clear

import delimited "...\fires_2005_VTP1km.csv", delimiter(",") encoding(UTF-8) clear 
drop cid
rename id_1k ID1
rename rastervalu agrifires2005
duplicates tag ID1, gen(dup)
br if dup==1
drop dup
 save "...\agrifires05_VTP1km.dta", replace

clear

import delimited "...\fires_2006_VTP1km.csv", delimiter(",") encoding(UTF-8) clear 
drop cid
rename id_1k ID1
rename rastervalu agrifires2006
duplicates tag ID1, gen(dup)
br if dup==1
drop dup
 save "...\agrifires06_VTP1km.dta", replace

clear

import delimited "...\fires_2007_VTP1km.csv"
drop cid
rename id_1k ID1
rename rastervalu agrifires2007
duplicates tag ID1, gen(dup)
br if dup==1
drop dup
 save "...\agrifires07_VTP1km.dta", replace

clear

import delimited "...\fires_2008_VTP1km.csv"
drop cid
rename id_1k ID1
rename rastervalu agrifires2008
duplicates tag ID1, gen(dup)
br if dup==1
drop dup
 save "...\agrifires08_VTP1km.dta", replace

clear

import delimited "...\fires_2009_VTP1km.csv"
drop cid
rename id_1k ID1
rename rastervalu agrifires2009
duplicates tag ID1, gen(dup)
br if dup==1
drop dup
 save "...\agrifires09_VTP1km.dta", replace

clear

import delimited "...\fires_2010_VTP1km.csv"
drop cid
rename id_1k ID1
rename rastervalu agrifires2010
duplicates tag ID1, gen(dup)
br if dup==1
drop dup
 save "...\agrifires10_VTP1km.dta", replace

clear


import delimited "...\fires_2012_VTP1km.csv"
drop cid
rename id_1k ID1
rename rastervalu agrifires2012
duplicates tag ID1, gen(dup)
br if dup==1
drop dup
 save "...\agrifires12_VTP1km.dta", replace

clear

import delimited "...\fires_2013_VTP1km.csv"
drop cid
rename id_1k ID1
rename rastervalu agrifires2013
duplicates tag ID1, gen(dup)
br if dup==1
drop dup
 save "...\agrifires13_VTP1km.dta", replace

clear

import delimited "...\fires_2014_VTP1km.csv"
drop cid
rename id_1k ID1
rename rastervalu agrifires2014
duplicates tag ID1, gen(dup)
br if dup==1
drop in 1028136
drop dup
 save "...\agrifires14_VTP1km.dta", replace

merge 1:1 ID1 using "...\agrifires13_VTP1km.dta", nogen
merge 1:1 ID1 using "...\agrifires12_VTP1km.dta", nogen
merge 1:1 ID1 using "...\agrifires10_VTP1km.dta", nogen
merge 1:1 ID1 using "...\agrifires09_VTP1km.dta", nogen
merge 1:1 ID1 using "...\agrifires08_VTP1km.dta", nogen
merge 1:1 ID1 using "...\agrifires07_VTP1km.dta", nogen
merge 1:1 ID1 using "...\agrifires06_VTP1km.dta", nogen
merge 1:1 ID1 using "...\agrifires05_VTP1km.dta", nogen
merge 1:1 ID1 using "...\agrifires04_VTP1km.dta", nogen
merge 1:1 ID1 using "...\agrifires03_VTP1km.dta", nogen
merge 1:1 ID1 using "...\agrifires02_VTP1km.dta", nogen
merge 1:1 ID1 using "...\agrifires01_VTP1km.dta", nogen


 save "...\agrifires_VTP1km.dta", replace

************ TMF forest cover losses

clear 

cd "...\TMF"
clear
local myfilelist : dir . files "*.csv"

foreach file of local myfilelist {
	drop _all
	insheet using `file'
	local outfile = subinstr("`file'",".csv","",.)
	save `outfile', replace
	}
 
local mydtalist : dir . files "*.dta"

foreach file of local mydtalist {
	use "`file'", clear
	local date = substr("`file'", 11,2)
	rename rastervalu TMFchange`date'
	drop cid
	rename id_1k ID1
	gen TMFtype`date'="undisturbed" if TMFchange==1
	replace TMFtype`date'="degraded" if TMFchange==2
	replace TMFtype`date'="deforested" if TMFchange==3
	replace TMFtype`date'="regrowth" if TMFchange==4
	replace TMFtype`date'="water" if TMFchange==5
	replace TMFtype`date'="other" if TMFchange==6
	drop if  TMFchange==.
	save `file', replace
}

clear

use "tmfchange_00_utm_vtp1km"
merge 1:1 ID1 using "tmfchange_01_utm_vtp1km", nogen
merge 1:1 ID1 using "tmfchange_02_utm_vtp1km", nogen
merge 1:1 ID1 using "tmfchange_03_utm_vtp1km", nogen
merge 1:1 ID1 using "tmfchange_04_utm_vtp1km", nogen
merge 1:1 ID1 using "tmfchange_05_utm_vtp1km", nogen
merge 1:1 ID1 using "tmfchange_06_utm_vtp1km", nogen
merge 1:1 ID1 using "tmfchange_07_utm_vtp1km", nogen
merge 1:1 ID1 using "tmfchange_08_utm_vtp1km", nogen
merge 1:1 ID1 using "tmfchange_09_utm_vtp1km", nogen
merge 1:1 ID1 using "tmfchange_10_utm_vtp1km", nogen
merge 1:1 ID1 using "tmfchange_11_utm_vtp1km", nogen
merge 1:1 ID1 using "tmfchange_12_utm_vtp1km", nogen
merge 1:1 ID1 using "tmfchange_13_utm_vtp1km", nogen
merge 1:1 ID1 using "tmfchange_14_utm_vtp1km", nogen
merge 1:1 ID1 using "tmfchange_15_utm_vtp1km", nogen

rename TMFchange00 TMFchange2000
rename TMFchange01 TMFchange2001
rename TMFchange02 TMFchange2002
rename TMFchange03 TMFchange2003
rename TMFchange04 TMFchange2004
rename TMFchange05 TMFchange2005
rename TMFchange06 TMFchange2006
rename TMFchange07 TMFchange2007
rename TMFchange08 TMFchange2008
rename TMFchange09 TMFchange2009
rename TMFchange10 TMFchange2010
rename TMFchange11 TMFchange2011
rename TMFchange12 TMFchange2012
rename TMFchange13 TMFchange2013
rename TMFchange14 TMFchange2014
rename TMFchange15 TMFchange2015

save "...\TMFchange_VTP1km.dta", replace

************************ merge all time varying data ***************************

clear

use  "...\wcprec_VTP1km.dta"

merge 1:1 ID1 using  "...\1km_PA.dta"
merge 1:1 ID1 using  "...\1km_PADDD.dta"
merge 1:1 ID1 using   "...\d1km_roads.dta"
merge 1:1 ID1 using  "...\lights_VTP1km.dta"
merge 1:1 ID1 using   "...\forest_VTP1km.dta"
merge 1:1 ID1 using  "...\wctavg_VTP1km.dta"
merge 1:1 ID1 using  "...\agrifires_VTP1km.dta"
merge 1:1 ID1 using   "...\CHprec_VTP1km.dta"
merge 1:1 ID1 using   "...\TMFchange_VTP1km.dta"

save "...\time_varying.dta", replace

********************************************************************************
************************* import and clean csv data ****************************
********************************* fixed ****************************************

************** import all csv, clean and drop duplicates, merge ****************

************ elevation 

import delimited "...\elevation_VTP1km.csv", delimiter(",") encoding(UTF-8) clear 
drop cid
rename id_1k ID1
rename rastervalu elevation

save "...\elevation_VTP1km.dta", replace

************ slopes
clear

import delimited "...\slope_VTP1km.csv", delimiter(",") encoding(UTF-8) clear 
drop cid
rename id_1k ID1
rename rastervalu slope

save "...\slope_VTP1km.dta", replace

************ land suitability 

clear

import delimited "...\landsuit_VTP1km.csv", delimiter(",") encoding(UTF-8) clear 
drop cid
rename id_1k ID1
rename rastervalu landsuit
duplicates tag ID1, gen(dup)
br if dup==1
drop in 1028018

save "...\landsuit_VTP1km.dta", replace


************ distance to cities 

clear

import delimited "...\d1km_cities.csv", delimiter(";") encoding(UTF-8) clear 
drop objectid
gen ID1 = in_fid+1
rename near_fid near_cities
rename near_dist dist_cities
destring dist_cities, dpcomma replace
replace dist_cities=dist_cities/1000

save "...\cities_d1km.dta", replace

************ distance to rivers 

clear
import delimited "...\d1km_rivers.csv", delimiter(";") encoding(UTF-8) clear 
drop objectid
gen ID1 = in_fid+1
rename near_fid near_rivers
rename near_dist dist_rivers
destring dist_rivers, dpcomma replace
replace dist_rivers=dist_rivers/1000

save "...\d1km_rivers.dta", replace

************ administrative frontiers

clear
import delimited "...\sec1km_adm.csv", delimiter(",") encoding(UTF-8) clear 
drop hasc_2 type_2 near_fid near_dist fid_sample cid
drop fid_iba_ad
rename id_1k ID1
rename id_1 ID_state
rename name_1 state
rename id_2 ID_mun
rename name_2 mun

save "...\sec1km_adm.dta", replace

************* distance to PAs 

clear

import delimited "...\d1km_wdpa.csv", delimiter(";") encoding(UTF-8) clear 
gen ID1 = in_fid+1
drop objectid in_fid
rename near_dist dist_PA
rename near_fid near_PA
destring dist_PA, dpcomma replace
replace dist_PA=dist_PA/1000

save "...\d1km_PA.dta", replace 

************* distance to PADDD

clear

import delimited "...\d1km_PADDD.csv", delimiter(";") encoding(UTF-8) clear 
gen ID1 = in_fid+1
drop objectid in_fid
rename near_dist dist_PADDD
rename near_fid near_PADDD
destring dist_PADDD, dpcomma replace
replace dist_PADDD=dist_PADDD/1000

save "...\d1km_PADDD.dta", replace 

*************************** merge all fixed data *******************************
clear

use "...\elevation_VTP1km.dta"

merge 1:1 ID1 using "...\slope_VTP1km.dta"
merge 1:1 ID1 using "...\cities_d1km.dta"
merge 1:1 ID1 using "...\d1km_rivers.dta"
merge 1:1  ID1 using "...\sec1km_adm.dta"
merge 1:1  ID1 using "...\d1km_PADDD.dta"
merge 1:1 ID1 using  "...\d1km_PA.dta"
merge 1:1 ID1 using  "...\landsuit_VTP1km.dta"

drop dup
drop objectid

save  "...\fixed.dta", replace

********************************************************************************
******************** merge fixed and time varying data *************************

use "...\fixed.dta"

merge 1:m ID1 using "...\time_varying.dta", nogen

save "cross_section.dta"

*************  PADDD characteristics

clear

import delimited "...\BA_utm_PADDD.csv", delimiter(",") encoding(UTF-8) clear 

rename fidn52 near_PADDD
rename year_padddc254 year_PADDD
rename protecte_1c254 namePADDD 
rename padddtrackn100 PADDD
rename location_kc254 location_k
rename typec254 typePADDD
rename statusc254 statusPADDD
rename year_gazetc254 year_gazet
rename causec254 causePADDD
rename area_padddc254 areaPADDD
rename size_pre_pc254 sizeprePADDD
rename size_post_c254 sizepostPADDD
rename iucn_categc254 IUCNprePADDD
rename iucn_cat_1c254 IUCNpostPADDD 
rename offset_c254 offsetPADDD
rename systemic_cc254 sys_change

drop geodatatypc254 regionc254 countryc254 iso_3166_1c254 wdpa_idc254 pa_coden1911 protectedc254  paddd_revec254 reversal_yc254 near_fidn100 near_distn1911

save "...\PADDD.dta", replace

clear

use "...\cross_section.dta"

merge m:m near_PADDD using "...\PADDD.dta", nogen

save  "...\cross_section.dta"

*************  PA characteristics

clear

import delimited "...\wdpa.csv", delimiter(",") encoding(UTF-8) clear 

rename fidn62 near_PA
rename wdpa_pidc80 IDwdpa
rename namec218 namePA
rename status_yrn100 yr_desigPa
rename desig_engc100 desigPA
rename iucn_catc80 IUCN_catPA
rename marinec80 marinePA
rename gov_typec80 govPA
rename mang_authc254 man_planPA

drop rep_arean1911 gis_arean1911 desig_typec80 pa_defc80 orig_namec218 desigc93 int_critc80 rep_m_arean1911 gis_m_arean1911 no_takec80 no_tk_arean1911 own_typec80 verifc80 metadataidn100 sub_locc80 parent_isoc80 iso3c80 near_fidn100 near_distn1911

save "...\PA.dta", replace

clear

use "...\cross_section.dta"

merge m:m near_PADDD using "...\PA.dta", nogen

save  "...\cross_section.dta"

********************************************************************************
******************************** clean data ***********************************
clear

use  "...\cross_section.dta"

drop if ID1==.

drop if treecover2000==-9999

foreach var in treecoverloss2000 treecoverloss2001 treecoverloss2002 treecoverloss2003 treecoverloss2004 treecoverloss2005 treecoverloss2006 treecoverloss2007 treecoverloss2008 treecoverloss2009 treecoverloss2010 treecoverloss2011 treecoverloss2012 treecoverloss2013 treecoverloss2014 treecoverloss2015 {
replace `var'=0 if `var'==.
  }
  
replace slope=. if slope==-9999
replace elevation=. if elevation==-9999
replace elevation=. if elevation<0
replace landsuit=. if landsuit==-9999

drop if marinePA!=0
drop marinePA
drop statusc80


drop if treecover2000==0

drop if Suit=="Water"


foreach var in _prec2001 _prec2002 _prec2003 _prec2004 _prec2005 _prec2006 _prec2007 _prec2009 _prec2010 _prec2011 _prec2012 _prec2013 _prec2014 _prec2015 _prec2000{
replace `var'=. if `var'==-9999
}

drop if ID1==.

replace ID_state=22 if (PADDD==696 | PADDD==695)

********************************************************************************
**************************** generate variables ********************************

******** covariates 

** 2001-2008 average rainfall
egen _prec0108 = rowmean(_prec2001 _prec2002 _prec2003 _prec2004 _prec2005 _prec2006 _prec2007 _prec2008) 

** PADDD characteristics
encode causePADDD, gen(cause)
encode statusPADDD, gen(status)
encode typePADDD, gen(type)

** states
encode state, gen(State)

** land suitability
gen suit="."
replace suit="Very high" if landsuit==1
replace suit="High" if landsuit==2
replace suit="Good" if landsuit==3
replace suit="Medium" if landsuit==4
replace suit="Moderate" if landsuit==5
replace suit="Marginal" if landsuit==6
replace suit="Water" if landsuit==9
rename suit Suit
encode Suit, gen(suit)

gen qual=0 if suit==3 | suit==4 | suit==5
replace qual=1 if suit==1 | suit==2 | suit==6

******* treatment variables 

** pixels reduced in size between 2009 and 2012
gen enacR_0912=.
replace enacR_0912=1  if dist_PADDD<1 & year_PADDD<2013 & year_PADDD>2008 & statusPADDD=="Enacted" & typePADDD!="downgrade"
replace enacR_0912=0 if !(dist_PADDD<1 & year_PADDD<2013 & year_PADDD>2008 & statusPADDD=="Enacted" & typePADDD!="downgrade") & (yr_desigPa<2009 & dist_PADDD>20 & dist_PA==0)

*10km buffer zone
gen enacR_0912_B3=.
replace enacR_0912_B3=1  if dist_PADDD<1 & year_PADDD<2013 & year_PADDD>2008 & statusPADDD=="Enacted" & typePADDD!="downgrade"
replace enacR_0912_B3=0 if !(dist_PADDD<1 & year_PADDD<2013 & year_PADDD>2008 & statusPADDD=="Enacted" & typePADDD!="downgrade") & (yr_desigPa<2009 & dist_PADDD>10 & dist_PA==0)

** pixels reduced in size between 2009 and 2012 before their reduction
gen Rprotected2000=. 
replace Rprotected2000=1 if  enacR_0912==1 & year_gazet<2001 
replace Rprotected2000=0 if !(enacR_0912==1 & year_gazet<2001) & (dist_PA>20 | (dist_PA==0 & yr_desigPa>2008)) & (dist_PADDD>20 | (dist_PADDD==0 & year_gazet>2008))

*10km buffer zone
gen Rprotected2000_B3=. 
replace Rprotected2000_B3=1 if  enacR_0912_B3==1 & year_gazet<2001 
replace Rprotected2000_B3=0 if !(enacR_0912_B3==1 & year_gazet<2001) & (dist_PA>10 | (dist_PA==0 & yr_desigPa>2008)) & (dist_PADDD>10 | (dist_PADDD==0 & year_gazet>2008))

** pixels never reduced in size 
gen NRprotected2000=.
replace NRprotected2000=1 if  (yr_desigPa<=2000 & dist_PADDD>20 & dist_PA==0)  | (year_gazet<=2000 & year_PADDD>2015 & dist_PADDD==0)
replace NRprotected2000=0 if !(enacR_0912==1 & year_gazet<2001) & (dist_PA>20 | (dist_PA==0 & yr_desigPa>2008)) & (dist_PADDD>20 | (dist_PADDD==0 & year_gazet>2008))

*10km buffer zone
gen NRprotected2000_B3=. 
replace NRprotected2000_B3=1 if  (yr_desigPa<=2000 & dist_PADDD>10 & dist_PA==0)  | (year_gazet<=2000 & year_PADDD>2015 & dist_PADDD==0)
replace NRprotected2000_B3=0 if !(enacR_0912_B3==1 & year_gazet<2001) & (dist_PA>10 | (dist_PA==0 & yr_desigPa>2008)) & (dist_PADDD>10 | (dist_PADDD==0 & year_gazet>2008))

******* outcome: GGC tree cover loss 

** from 2001 to 2008,  2009 to 2012, 2011_2015  
egen defo_0108 = rowmax (treecoverloss2001 treecoverloss2002 treecoverloss2003 treecoverloss2004 treecoverloss2005 treecoverloss2006 treecoverloss2007 treecoverloss2008)
egen defo_0912 = rowmax (treecoverloss2009 treecoverloss2010 treecoverloss2011 treecoverloss2012
egen defo_1115=rowmax(treecoverloss2011 treecoverloss2012 treecoverloss2013 treecoverloss2014 treecoverloss2015)

** from 2010 to 2015 :
// outcome in 2013-2015 + outcome in 2010-2012.
// some size reduction occurs in 2010-2012 so we create defo_p0912 to count deforested pixels falling in it.

gen defo_1015=.

egen defo_1315=rowmax(treecoverloss2013 treecoverloss2014 treecoverloss2015)
replace defo_1015=defo_1315

gen max_loss0912 = "." 
egen yr_loss0912 = rowmax (treecoverloss2009 treecoverloss2010 treecoverloss2011 treecoverloss2012)
unab xvars: treecoverloss2009 treecoverloss2010 treecoverloss2011 treecoverloss2012
 foreach x of local xvars { 
    replace max_loss0912 = "`x'" if `x' == yr_loss0912
}
replace max_loss0912="2009" if  max_loss=="treecoverloss2009"
replace max_loss0912="2010" if  max_loss=="treecoverloss2010"
replace max_loss0912="2011" if  max_loss=="treecoverloss2011"
replace max_loss0912="2012" if  max_loss=="treecoverloss2012"
rename max_loss0912 year_loss0912
drop yr_loss0912

destring year_loss0912, replace
replace year_loss0912=. if defo_0912==0
replace defo_1015=1 if year_loss0912>=2010 & year_PADDD==. & defo_0912==1 & year_loss0912!=. 
replace defo_1015=1 if year_loss0912>year_PADDD & enacR_0912==1 & year_loss0912!=.  & year_PADDD!=.

gen defo_p0912=.
replace defo_p0912=1 if year_PADDD>=year_loss0912 & enacR_0912==1 & year_loss0912!=. & year_PADDD!=.
replace defo_p0912=1 if year_loss0912<=2012 & year_loss0912!=. & enacR_0912==0 & year_PADDD==.


******* outcome: TMF tree cover loss 

** pixel with forests in 2000
gen undis=1 if TMFchange2000==1

** pixels =1 if deforested or degradaded between 2001 and 2015
forvalues i=2001/2015 {
	gen TMF_`i'=1 if (TMFchange`i'==2 | TMFchange`i'==3) & undis==1
	replace TMF_`i'=0 if TMFchange`i'==1  & undis==1
}

** come back to 0 after the deforestation or degradation event 
replace TMF_2015=0 if TMF_2014==1
replace TMF_2014=0 if TMF_2013==1
replace TMF_2013=0 if TMF_2012==1
replace TMF_2012=0 if TMF_2011==1
replace TMF_2011=0 if TMF_2010==1
replace TMF_2010=0 if TMF_2009==1
replace TMF_2009=0 if TMF_2008==1
replace TMF_2008=0 if TMF_2007==1
replace TMF_2007=0 if TMF_2006==1
replace TMF_2006=0 if TMF_2005==1
replace TMF_2005=0 if TMF_2004==1
replace TMF_2004=0 if TMF_2003==1
replace TMF_2003=0 if TMF_2002==1
replace TMF_2002=0 if TMF_2001==1

** total 
egen TMF_1115=rowmax(TMF_2011 TMF_2012 TMF_2013 TMF_2014 TMF_2015)
egen TMF_1315=rowmax(TMF_2013 TMF_2014 TMF_2015)
egen TMF_1015=rowmax(TMF_2010 TMF_2011 TMF_2012  TMF_2013 TMF_2014 TMF_2015)
egen TMF_0912=rowmax(TMF_2009 TMF_2010 TMF_2011 TMF_2012)


save  "...\PADDD_impact.dta"


